/**
 * 
 */
package com.and.xmppsdk.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.drl.xmpp.Contact;
import org.drl.xmpp.RosterManager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.and.xmppsdk.R;

/**
 * @author hljdr@gmail.com
 * 
 */
public class ContactGroupAdapter extends
		android.widget.BaseExpandableListAdapter {

	private Context mContext;
	private LayoutInflater inflater;
	private RosterManager rm;
	private HashMap<String, List<Contact>> mGroupContacts;
	private List<String> keys;

	public ContactGroupAdapter(Context ctx, RosterManager rosterManager) {
		mContext = ctx;
		inflater = LayoutInflater.from(mContext);
		rm = rosterManager;
		mGroupContacts = rm.getGropusToHahsMap();
		Set<String> set = mGroupContacts.keySet();
		keys = new ArrayList<String>();
		for (String s : set) {
			keys.add(s);
		}
	}

	@Override
	public Contact getChild(int groupPosition, int childPosition) {
		return mGroupContacts.get(keys.get(groupPosition)).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View v, ViewGroup parent) {
		if (v == null) {
			v = inflater.inflate(R.layout.adapter_group_contact_child, parent,
					false);
		}
		Contact mCm = getChild(groupPosition, childPosition);
		ContactAdapter.setupContactViewItem(v, mCm);
		return v;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mGroupContacts.get(keys.get(groupPosition)).size();
	}

	@Override
	public String getGroup(int groupPosition) {
		return keys.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return keys.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view,
			ViewGroup parent) {
		if (view == null) {
			view = inflater.inflate(R.layout.adapter_group_contact_groupview,
					parent, false);
		}
		TextView name = (TextView) view.findViewById(R.id.groupname);
		String lablel = getGroup(groupPosition);
		name.setText(lablel);
		return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
